<?php

namespace app\api\controller;
use app\api\controller\Student as StudentModel;
use think\facade\Filesystem;

class  Student
{
    //模拟卷第四题PHP进阶知识
    public $studentData;

    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token = input('get.token');
        if (empty($token) || $token == '-1') output_error('token为空');
        $this->studentData = StudentModel::where('student_token','=',$token)->find();
        if (empty($this->studentData)) output_error('token无效，请重新登录');

    }

    //注册功能
    public function getData()
    {
        //输出学生的所有信息，去除“student_password”字段
        unset($this->studentData['student_password']);
        output_data($this->studentData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $name = input('get.name');
        if (empty($name)) {
            output_error('信息填写不完整。请填写完整');
            StudentModel::where('student_id','=',$this->studentData['student-id'])->update(array('student_name' =>$this->studentData));
            output_data('1');
        }
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        //学生头像文件唯一，更新时需要删除原有的文件。
        $file = request()->file('image');
        $name = Filesystem::disk('avatar')->putFile('',$file,'uniqid');

        if ($this->studentData['student_avatar'] != 'default.png') {
            $filename = app()->getRootPath().'public\\upload\\avatar\\' . $this->studentData['student-id'];

        }
    }

}
